import { createRouter, createWebHistory } from 'vue-router'


import { ElMessage } from 'element-plus'

const routes = [
    {
        path: '/',
        name: 'Login',
        component: () =>
            import('../views/Login.vue')
    },
    {
        path: '/home',
        name: 'Home',
        component: () =>
            import('../views/Home.vue')
    },
    {
        path: '/rider',
        name: 'Rider',
        component: () => import('../views/rider/Rider.vue')
    },
    {
        path: '/detail',
        name: 'Detail',
        component: () => import('../views/product/Detail.vue')
    },
    {
        path: '/purchase',
        name: 'Purchase',
        component: () => import('../views/purchase/Purchase.vue')
    },
    {
        path: '/order',
        name: 'Order',
        component: () => import('../views/order/Order.vue')
    },
    {
        path: '/notice',
        name: 'Notice',
        component: () => import('../views/notice/Notice.vue')
    },
]
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.name == '/' || to.name == 'Login') {
        localStorage.removeItem('userinfo')
        next()
    } else {
        let userinfo = localStorage.getItem('userinfo')
        if (userinfo == null || userinfo == " ") {
            router.replace("/")
            return ElMessage({
                message: "登录信息以过期，请重新登录！",
                type: "error",
                duration: 1000
            })
        }
    }

    next()
})

export default router